﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ReportManagement;
using ToppingControl.Models;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace ToppingControl.Controllers
{
   public class PrintBarcodeController : PdfViewController
    {


      public void showRPT()
        {
            try
            {                                
                bool isValid = true;
 
                string strReportName = System.Web.HttpContext.Current.Session["rptName"].ToString();
                string strempCode = System.Web.HttpContext.Current.Session["empCode"].ToString();
                var rptSource = System.Web.HttpContext.Current.Session["rptSource"];                
 
                if (string.IsNullOrEmpty(strReportName)) 
                {
                    isValid = false;
                }
 
                if (isValid)
                {
                    ReportDocument rd = new ReportDocument();
                    string strPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "rpt//" + strReportName;
                    rd.Load(strPath);
 
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);
                    if (!string.IsNullOrEmpty(empCode))
                        rd.SetParameterValue("empCode", strempCode);
                    
                    
                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                    
                    // Clear all sessions value
                    Session["rptName"] = null;
                    Session["empCode"] = null;
                    Session["rptSource"] = null;
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }


        
         //GET: /PrintBarcode/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PrintBefore()
        {
    
           TopCordActModel mdSearch = new TopCordActModel();
           var obj = mdSearch.GET_ACT_FORTOP(" Top 1 ");
           //return View(obj[0]);
           return this.ViewPdf("PrintBefore", "PrintBefore", obj[0]);
        }



        public FileContentResult SomeImage(string code)
        {
           return File(QRCodeHtmlHelper.enCode_barcode(code,3,3), "image/jpeg");
        }

    }
}
